﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.BodyWeight;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepWeightCommands
{
    public class PhaseCreateHandler : IRequestHandler<PhaseCreateCommand, Result>
    {
        private readonly IBaseRepository<PhaseDeters> PhasebaseRepository;
        private readonly IMapper mapper;

        public PhaseCreateHandler(IBaseRepository<PhaseDeters> phasebaseRepository, IMapper mapper)
        {
            PhasebaseRepository = phasebaseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 品相测定添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PhaseCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.add };
            PhaseDeters phaseDeters = mapper.Map<PhaseDeters>(request);
            var res = PhasebaseRepository.GetAll().Where(x => x.EarNumber == request.EarNumber).FirstOrDefault();
            //判断是否有数据，判断唯一性
            if (res != null)
            {
                result.Code = -1;
            }
            else
            {
                var list = PhasebaseRepository.Add(phaseDeters);
                result.Code = list;
            }

            return result;
        }
    }
}
